package com.jied.duodian.service.Impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.jied.duodian.dao.DdUserDAO;
import com.jied.duodian.entity.DdOrderExample;
import com.jied.duodian.entity.DdUser;
import com.jied.duodian.entity.DdUserExample;
import com.jied.duodian.entity.Dutity.Page;
import com.jied.duodian.service.DdUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/10/28
 * WWW https://github.com/j6l
 */
@Service
@Slf4j
@Transactional
public class DdUserServiceImpl implements DdUserService {

    @Autowired
    DdUserDAO ddUserDAO;

    /**
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = "qt", keyGenerator = "keyGenerator")
    public DdUser getUserByid(Integer id) {
        return ddUserDAO.selectByPrimaryKey( id );
    }

    @Override
    public DdUser getUserByName(String name) {
        DdUserExample example = new DdUserExample();
        DdUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo( name );
        List<DdUser> list = ddUserDAO.selectByExample( example );
        return list.size() > 0 ? list.get( 0 ) : null;
    }

    /**
     * id 0
     * name 1
     *
     * @param ddUser
     * @return
     */
    @Override
    public Integer updataUserByid(DdUser ddUser) {
        if (ddUser.getPassword().length() >= 6) {
            ddUser.setPassword( new BCryptPasswordEncoder().encode( ddUser.getPassword() ) );
        }
        return ddUserDAO.updateByPrimaryKeySelective( ddUser );
    }

    @Override
    public Integer delUser(Integer id) {
        return ddUserDAO.deleteByPrimaryKey( id );
    }

    @Override
    public Integer addUserList(DdUser ddUsers) {
        Integer ll = ddUserDAO.insertSelective( ddUsers );
        return ll;
    }

    /**
     * 全部 0
     * nickname 1
     * name 2
     * phone 3
     *
     * @param ddUser
     * @param page
     * @param i
     * @return
     */
    @Override
    public Page getUserLista(DdUser ddUser, Page page, int i) {
        // log.info( "第一次page{}", page );
        DdUserExample example = new DdUserExample();
        DdUserExample.Criteria criteria = example.createCriteria();
        if (i == 0) criteria.andIdIsNotNull();
        if (i == 1) criteria.andNicknameLike( "%" + ddUser.getNickname() + "%" );
        if (i == 2) criteria.andUsernameLike( "%" + ddUser.getUsername() + "%" );
        if (i == 3) criteria.andPhoneLike( "%" + ddUser.getPhone() + "%" );
        long count = ddUserDAO.countByExample( example );
        page.setTotal( count );
        example.setLimit( page.getSize() );
        example.setOffset( Long.valueOf( page.getSize() * (page.getPageindex() - 1) ) );
        List<DdUser> list = ddUserDAO.selectByExample( example );
        for (DdUser user : list) {
            user.setPassword( "" );
        }
        page.setContent( list );
        // log.info( "第二次page{}", page );
        return page;
    }

    @Override
    public Long getuserall() {
        DdUserExample example = new DdUserExample();
        long count = ddUserDAO.countByExample( example );
        return count;
    }

    @Override
    public Long getuserzt() {
        final LocalDateTime new1 = LocalDateTimeUtil.beginOfDay( LocalDateTime.now() );
        final LocalDateTime new2 = LocalDateTimeUtil.offset( new1, -1, ChronoUnit.DAYS );
        long count = getCount( new1, new2 );
        return count;
    }

    private long getCount(LocalDateTime firstday, LocalDateTime firstday1) {
        DdUserExample example = new DdUserExample();
        DdUserExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeGreaterThanOrEqualTo( Date.from( firstday.atZone( ZoneId.systemDefault() ).toInstant() ) );
        criteria.andCreateTimeLessThanOrEqualTo( Date.from( firstday1.atZone( ZoneId.systemDefault() ).toInstant() ) );
        return ddUserDAO.countByExample( example );
    }

    @Override
    public Long getusernojihuo() {
        DdUserExample example = new DdUserExample();
        DdUserExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo( 0 );
        long count = ddUserDAO.countByExample( example );
        return count;
    }
}
